package com.jepusi.exspringboot;

import com.alibaba.fastjson.JSONObject;
import org.apache.poi.ss.usermodel.*;


import java.io.*;
import java.util.ArrayList;

public class SecurityToolsTest {


    public void test() {
        try {
            // 指定目录路径
            File directoryPath = new File("D:\\brxx\\ex");
            BufferedWriter writer = new BufferedWriter(new FileWriter(directoryPath+"\\file.txt"));
            // 列出该目录下的所有文件
            File[] filesList = directoryPath.listFiles();

            if (filesList != null) {
                for (File file : filesList) {
                    if (file.isFile() && file.getName().endsWith(".xlsx")) {
                        // 读取 Excel 文件
                        FileInputStream inputStream = new FileInputStream(file);
                        Workbook workbook = WorkbookFactory.create(inputStream);

                        // 获取第一个工作表
                        Sheet sheet = workbook.getSheetAt(0);
                        int i = 0;
                        // 遍历工作表中的行和列
                       // ArrayList<Transaction> list = new ArrayList<>();
                        String zhanghao = "";
                        String huming = "";
                        String kahao = "";
                        String huobi = "";

                        for (Row row : sheet) {
                            i++;
                            if(i==2){
                                for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) {
                                    if(row.getCell(j).toString().equals("账号")){
                                        zhanghao = row.getCell(j+1).toString();
                                    }
                                    if(row.getCell(j).toString().equals("户名")){
                                        huming = row.getCell(j+1).toString();
                                    }
                                    if(row.getCell(j).toString().equals("卡号")){
                                        kahao = row.getCell(j+1).toString();
                                    }
                                    if(row.getCell(j).toString().equals("货币")){
                                        huobi = row.getCell(j+1).toString();
                                    }
                                }

                            }
                            //System.out.println(zhanghao+" "+huming+" "+kahao+" "+huobi);
                            if (i >= 4) {
                                StringBuilder builder = new StringBuilder();
                                //Transaction transaction = new Transaction();
                                builder.append(zhanghao+",");
                                builder.append(huming+",");
                                builder.append(kahao+",");
                                builder.append(huobi+",");
                                for (Cell cell : row) {
                                    // 输出单元格内容
                                    //System.out.print(cell.toString() + "\t");
                                    builder.append(cell.toString() + ",");
                                }

                                String builderResult = builder
                                        .deleteCharAt(builder.toString().length()-1).toString();
                                System.out.println(builderResult);
                                writer.write(builderResult);
                                writer.newLine();
                                //System.out.println(builderResult.length());
//                                String[] resultStr = builder.toString().substring(0, builderResult.length() - 2).split(",");
//                               // Arrays.toString(resultStr);
//                                transaction.setTransNo(resultStr[0]);
//                                transaction.setDC(resultStr[1]);
//                                transaction.setTransDate(resultStr[2]);
//                                transaction.setTransDatetime(resultStr[3]);
//                                transaction.setTransSalary(Double.valueOf(resultStr[4]));
//                                transaction.setUseSum(Double.valueOf(resultStr[5]));
//                                transaction.setTransOrg(resultStr[6]);
//                                list.add(transaction);
//                                //System.out.println(builder.toString());

                            }

                        }
                        //list.forEach(str -> System.out.println(str + "\t"));
                        // 关闭流和工作簿
                        workbook.close();
                        inputStream.close();

                    }
                }
            }
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
